package com.softtown.modules.sys.service.impl;

import com.softtown.common.exception.ErrorCode;
import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.common.utils.Result;
import com.softtown.modules.security.user.SecurityUser;
import com.softtown.modules.sys.dao.CompComDocAttDao;
import com.softtown.modules.sys.dto.CompComDocAttAddDTO;
import com.softtown.modules.sys.entity.CompComDocAttEntity;
import com.softtown.modules.sys.service.CompComDocAttService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 系统单据附件实现类
 *
 * @author Dingyayong
 * @version v1.0
 * @create 2019-10-09 16:33
 **/
@Service
public class CompComDocAttServiceImpl extends BaseServiceImpl<CompComDocAttDao, CompComDocAttEntity> implements CompComDocAttService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByDocId(Long docId, Long compId) {
        baseDao.deleteByDocIdAndCompId(docId, compId);
    }

    @Override
    public List<CompComDocAttEntity> getSysDocAttEntityByDocId(Long docId, Long compId) {
        return baseDao.selectDocAttByDocId(docId, compId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteDocAttById(Long id, Long compId) {
        Result result = new Result<>();
        int count = baseDao.deleteByIdAndCompId(id, compId);
        if (count <= 0) {
            return result.error(ErrorCode.DELETE_FAILED);
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteDocAttByUrl(Long compId, String docTypeValue, Long docId, String attUrl) {
        Result result = new Result<>();
        CompComDocAttEntity compComDocAttEntity = baseDao.selectDocAttByUrl(compId, docTypeValue, docId, attUrl);
        if (compComDocAttEntity != null) {
            compComDocAttEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
            compComDocAttEntity.setIsDeleted(null);
            compComDocAttEntity.setUpdater(SecurityUser.getUserId());
            compComDocAttEntity.setUpdateDate(new Date());
            baseDao.updateDocAtt(compComDocAttEntity);
        } else {
            return result.error(ErrorCode.DELETE_FAILED);
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result addDocAtt(CompComDocAttAddDTO compComDocAttAddDTO) {
        CompComDocAttEntity comDocAttEntity = baseDao.getByDocIdAndCompIdByLinNo(compComDocAttAddDTO.getDocId(), compComDocAttAddDTO.getCompId());
        CompComDocAttEntity compComDocAttEntity = new CompComDocAttEntity();
        compComDocAttEntity.setAttName(compComDocAttAddDTO.getAttName());
        compComDocAttEntity.setCompId(compComDocAttAddDTO.getCompId());
        compComDocAttEntity.setAttUrl(compComDocAttAddDTO.getAttUrl());
        compComDocAttEntity.setCreateDate(new Date());
        if (comDocAttEntity != null) {
            compComDocAttEntity.setSctnLineNo(comDocAttEntity.getSctnLineNo() + 1);
        } else {
            compComDocAttEntity.setSctnLineNo(1);
        }
        compComDocAttEntity.setCreator(SecurityUser.getUser().getId());
        compComDocAttEntity.setUpdateDate(new Date());
        compComDocAttEntity.setUpdater(SecurityUser.getUser().getId());
        compComDocAttEntity.setDocTypeValue(compComDocAttAddDTO.getDocTypeValue());
        compComDocAttEntity.setDocId(compComDocAttAddDTO.getDocId());
        compComDocAttEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
        baseDao.insert(compComDocAttEntity);
        return new Result<>();
    }
}

